# Maintainer: David Runge <dvzrv@archlinux.org>

pkgbase=libtracefs
pkgname=(libtracefs libtracefs-docs)
pkgver=1.6.4
pkgrel=2
pkgdesc="Linux kernel trace file system library"
arch=(loong64 x86_64)
url="https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/about/"
license=(GPL2 LGPL2.1)
makedepends=(asciidoc libtraceevent xmlto)
source=(
  $pkgname-$pkgver.tar.gz::https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/snapshot/$pkgname-$pkgver.tar.gz
  $pkgname-1.2.0-docs_makefile.patch
  $pkgname-1.6.1-ldflags.patch
)
sha512sums=('0db20393272ce75c9132fc189993929545f41ac00cc9b0fdbb6adcb4bcda41433f05c047d06d9774f9b00b9e3ed3cfed3879e75365e72c0bb705cf16449e06c4'
            'bdfffba9957d889c60baea9c197709bf1a62d30ac4b8fd47d930b8d5b67fa040817b9a835e226dd1201f2ccdb7fcb43a12093c2b559fb6c0edc3e9ba167062c2'
            '3f83a85aa0be92ff4b1b5e9fe484f84305c149f0b4b7a2785aac25d3af098a2dbaaed72c470e65efe665be3eca4531ad5687f51688bc0ebb6b92f20da20b5d76')
b2sums=('24ca0affbd3343429c096795ea25fca6b96c5e806fff197149db290a4b3ed4ad642738a74d395c025b2a5813d9a14ada9b36be8bc0bdbc31d110542fb49ce1ae'
        '1340af2c65dc344912715c69827d5849fda83e4bcf136da3ccdbee4e3b508e9b557af0828914dff8d27abebbdbdab975d8800432d5252b6b1eb0da08b0cb4b02'
        '2663392d56cf1d811fdc8d117a5455c32c4c5f5a18a8e3aa4ee44df7b350bdd91fd512d4753ca34286ee59c4e7f4b8309bfc62b9711b1a01e60d90de91995418')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  patch -d $pkgname-$pkgver -Np 1 -i ../$pkgname-1.2.0-docs_makefile.patch
  patch -d $pkgname-$pkgver -Np 1 -i ../$pkgname-1.6.1-ldflags.patch
}

build() {
  make -C $pkgname-$pkgver
  make sqlhist -C $pkgname-$pkgver
  make doc -C $pkgname-$pkgver
}

package_libtracefs() {
  depends+=(glibc libtraceevent.so)
  optdepends=('libtracefs-docs: for documentation')
  provides=(libtracefs.so)

  make libdir_relative=lib prefix=/usr DESTDIR="$pkgdir/" install -C $pkgname-$pkgver
  make libdir_relative=lib prefix=/usr DESTDIR="$pkgdir/" install -C $pkgname-$pkgver/Documentation
  install -vDm 755 $pkgname-$pkgver/bin/sqlhist -t "$pkgdir/usr/bin/"

  (
    cd "$pkgdir"
    _pick libtracefs-docs usr/share/doc
  )
}

package_libtracefs-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
}
